/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ProgressSupport.java
*
* Created on December 11, 2007, 9:46 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.swing.components;

import java.awt.Toolkit;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 *
 * @author stewari1
 */
public class ProgressSupport {

    /**
     * A binding property. thorws "overAllProgressChanged" when this property is
     * changed using its setter method
     */
    public static final String PROP_OVERALL_PROGRESS = "overAllProgress";

    /**
     * bound property for status information
     */
    public static final String PROP_STATUS_INFO = "statusInfo";

    /**
     * bound property for status information
     */
    public static final String PROP_LOG_INFO = "log";

    /**
     * bound property for status information
     */
    public static final String PROP_OVERALL_STATUS = "overAllStatus";

    /**
     * bound property for progress information to be used in a progressbar
     */
    public static final String PROP_PROGRESS = "progress";

    /**
     * bound property for details information to be used in a progressbar
     */
    public static final String PROP_DETAILS_ASKED = "detailsAsked";

    /**
     * bound indicator property for cancel information to be used in a progressbar
     */
    public static final String PROP_CANCEL = "cancel";

    /**
     * bound property to pop and close the progressbar
     */
    public static final String PROP_SHOW_PROGRESS = "showProgress";

    /**
     * bound property to pop and close the overallprogressbar
     */
    public static final String PROP_SHOW_OVERALL_PROGRESS = "showOverAllProgress";

    /** Field description */
    protected PropertyChangeSupport propertySupport = null;

    /** Field description */
    private int overAllProgress = 0;

    /** Field description */
    private String log = "";

    /** Field description */
    private boolean showProgress = false;

    /** Field description */
    private boolean showOverAllProgress = false;

    /** Field description */
    private boolean logEnabled = false;

    /** Field description */
    private String statusInfo;

    /** Field description */
    private String overAllStatus;

    /** Field description */
    private int progress;

    /** Field description */
    private boolean detailsAsked;

    /**
     * Constructs ...
     */
    protected ProgressSupport() {
        this.propertySupport = new PropertyChangeSupport(this);
    }

    /**
     * Constructs ...
     *
     * @param source description
     */
    public ProgressSupport(ProgressSupportable source) {
        this.propertySupport = new PropertyChangeSupport(source);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(propertyName, listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }

    public void setLog(String str) {

        if (!logEnabled) {
            return;
        }

        this.log += str;

        propertySupport.firePropertyChange(PROP_LOG_INFO, "", log);
    }

    public void setShowProgress(boolean val) {
        propertySupport.firePropertyChange(PROP_SHOW_PROGRESS, !val, val);
    }

    public void setShowOverAllProgress(boolean val) {
        propertySupport.firePropertyChange(PROP_SHOW_OVERALL_PROGRESS, !val, val);
    }

    public void setCancel() {
        propertySupport.firePropertyChange(PROP_CANCEL, 0, 1);
    }

    public void setDetailsAsked(boolean detailsAsked) {

        this.detailsAsked = detailsAsked;

        propertySupport.firePropertyChange(PROP_DETAILS_ASKED, !detailsAsked, detailsAsked);
    }

    public void setStatusInfo(String statusInfo) {

        String oldValue = this.statusInfo;

        this.statusInfo = statusInfo;

        propertySupport.firePropertyChange(PROP_STATUS_INFO, oldValue, statusInfo);
    }

    public void setHtmlStatusInfo(String statusInfo) {
        setStatusInfo("<html>" + statusInfo + "</html>");
    }

    public void setProgress(int progress) {

        int oldValue = this.progress;

        this.progress = progress;

        propertySupport.firePropertyChange(PROP_PROGRESS, oldValue, progress);
    }

    protected void setOverAllProgress(int overAllProgress) {

        int oldValue = this.overAllProgress;

        this.overAllProgress = overAllProgress;

        propertySupport.firePropertyChange(PROP_OVERALL_PROGRESS, oldValue, overAllProgress);
    }

    public void setOverAllStatus(String overAllStatus) {

        String oldValue = this.overAllStatus;

        this.overAllStatus = overAllStatus;

        propertySupport.firePropertyChange(PROP_OVERALL_STATUS, oldValue, overAllStatus);
    }

    protected static void beep() {
        Toolkit.getDefaultToolkit().beep();
    }

    public static void sleep(int secs) {

        try {
            Thread.sleep(secs * 1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    public String getLogString() {
        return log;
    }

    public boolean isLogEnabled() {
        return logEnabled;
    }

    public void setLogEnabled(boolean logEnabled) {
        this.logEnabled = logEnabled;
    }
}
